package com.zhijiang.hospital.controller;

import com.alibaba.excel.EasyExcel;
import com.zhijiang.hospital.entity.ComDeptBedsnum;
import com.zhijiang.hospital.mapper.ComDeptBedsnumMapper;
import com.zhijiang.hospital.service.ComDeptBedsnumService;
import com.zhijiang.util.BaseQuery;
import com.zhijiang.util.PageList;
import com.zhijiang.util.Result;
import com.zhijiang.util.TimeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author 75936
 * @version 1.0
 * @project zhijiang_hospital-data
 * @description
 * @date 2022-10-25 10:42:26
 */

@Api(value = "住院运行数据", tags = {"住院运行数据"})
@Slf4j
@RestController
@RequestMapping("InpatientOperation")
public class InpatientOperationDataController {

    @Autowired
    private ComDeptBedsnumService comDeptBedsnumService;
    @Autowired
    ComDeptBedsnumMapper comDeptBedsnumMapper;


    @ApiOperation("住院运行数据分页")
    @PostMapping("/findComDeptBedsNum")
    public Result findComDeptBedsNum(@RequestBody BaseQuery baseQuery) {
        List<ComDeptBedsnum> comDeptBedsNum = comDeptBedsnumService.findComDeptBedsNum(TimeUtil.initBaseQuery(baseQuery));
        PageList<ComDeptBedsnum> comDeptBedsNumByPageList = comDeptBedsnumService.findComDeptBedsNumByPageList(TimeUtil.initBaseQuery(baseQuery));
        Map<String, Object> map = new HashMap<>();
        map.put("jihe", comDeptBedsNumByPageList);
        map.put("heji", comDeptBedsNum);
        return Result.ok(map);
    }

    @ApiOperation("住院运行数据分页excel表格导出")
    @GetMapping("loadExcel")
    public void exportExcelPeopleNucleicEmployees(String startTime, String endTime, HttpServletResponse response) throws IOException {
        BaseQuery baseQuery = new BaseQuery();
        baseQuery.setStartTime(startTime);
        baseQuery.setEndTime(endTime);
        BaseQuery baseQuery1 = TimeUtil.initBaseQuery(baseQuery);
        List<ComDeptBedsnum> list = comDeptBedsnumMapper.findAll(baseQuery1.getStartTime(), baseQuery1.getEndTime());
        log.error("数据有误", list);
        list.forEach(System.out::println);
        String title = "住院运行数据";
        // 告诉浏览器用什么软件可以打开此文件
        response.setHeader("content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        // 下载文件的默认名称
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("住院运行数据.xlsx", "utf-8"));
        //4. 创建输入、输出流
        EasyExcel.write(response.getOutputStream(), ComDeptBedsnum.class).sheet(title).doWrite(list);
    }


    @GetMapping("findBrokenLine")
    @ApiModelProperty(value = "住院运行数据折线图")
    @ApiOperation("住院运行数据折线图")
    public Result loadDateChart() {
        return Result.ok(comDeptBedsnumService.loadDateChart());
    }

    @GetMapping("loadYear")
    @ApiModelProperty(value = "今年住院运行数据对比")
    @ApiOperation("今年住院运行数据对比")
    public Result loadYear() {
        List<ComDeptBedsnum> list = comDeptBedsnumService.loadYear();
        return Result.ok(list);
    }


    @GetMapping("loadLastYear")
    @ApiModelProperty(value = "去年住院运行数据对比")
    @ApiOperation("去年住院运行数据对比")
    public Result loadLastYear() {
        List<ComDeptBedsnum> list = comDeptBedsnumService.loadLastYearData();
        return Result.ok(list);
    }

}
